$(function(){
	  var resourceType={
		min:'分钟',
		ite:'条',
		ome:'兆',
		pem:'人月'
	  },originalType={
	    BUY:'购买',
	    GIV:'赠送'
	  };
	  
	  function transformCache(rs){
		  var data=[],elem,custAccount,appData;
		  for (var i = 0; i < rs.length; i++) {
			custAccount=rs[i];
			elem={
			  name:decodeURI(custAccount.ext_orgName),
			  cust_account:custAccount.cust_account,
			  account_balance:custAccount.credit_balance+'元',
			  resources:[]
			};
			for (var m = 0; m < custAccount.appdata.length; m++) {
				appData=custAccount.appdata[m];			
				for (var k = 0; k < appData.resources.length; k++) {
					var rs0=appData.resources[k];
					$.extend(rs0,{
						product_name:decodeURI(rs0.product_name),
						balance:rs0.resuource_num+resourceType[rs0.measure_type],
						resource_type:originalType[rs0.buy_type],
						appName:decodeURI(appData.app_name)
					})
				}
				Array.prototype.push.apply(elem.resources,appData.resources);
			}
			data.push(elem);
		}
		sessionStorage.setItem('correlationaccount',JSON.stringify(data));    		  
	  }
	  
	  var telephoneElem=$('#telephone'),
	  paramJSON=utils.createQueryStringJSONObject(location.search.substr(1),true),
	  smscode=$('#smscode'),
	  submitBtn=$('#unified_ok').on('click',function(){
		var captcha=smscode.val(),
		telephone = telephoneElem.val(),
		unionCode=$('#code').val(),
		result;
		if(telephone===''){//未输入手机号
	    	new UserPrompt('请输入手机号码');
            return;
		}
	    result=utils.checkTelephoneNumber(telephone);
	    if (!result.success) {//手机号校验未通过
	    	new UserPrompt(result.msg);
	        return;
	    }
		if(unionCode===''){//未输入关联码
	    	new UserPrompt('请输入关联码');
	        return;
		}
		if(captcha===''){//未输入验证码
	    	new UserPrompt('请输入验证码');
	        return;
		}
		if(captcha.length!==6){//验证码为6位数字
	    	new UserPrompt('请输入正确的验证码');
			return;
		}
	    $.ajax({
	      type : "get",
          dataType : "jsonp",
	      url : "http://223.202.119.13:8090/account/qryMergeableOrgList.do",
	      data : $.extend(paramJSON,{
	    	  verifyCode:captcha,
	    	  ext_orgReleCode:unionCode,
	    	  ext_orgPhone:telephone
	      }),
	      success:function(data){
	    	  console.log('querylist=='+JSON.stringify(data));
	    	  if(data.result==='0'){
	    		  sessionStorage.setItem('parameter',JSON.stringify(paramJSON));
	    		  sessionStorage.setItem('status',data.status);
	    		  if(data.status==='0001'){//需要合并账户
	                  transformCache(data.data);
	    		  }   		
	    		  locaion.assign('updateIndex.html');
	    	  }else{
			        new UserPrompt(data.discripe);
	    	  }    	  
	      }
	    });
	  }),
	  code=$('#sendCaptcha');
	  $('#relcode').text(paramJSON.relcodelabel+"：");
	  $('#code').attr('placeholder',paramJSON.appname+'关联时的认证码').val(paramJSON.ext_orgReleCode||'').focus();
	  $('#telephone').val(paramJSON.ext_orgPhone||'');
	  code.on('click', function() {
	    var telephone = telephoneElem.val(),result,times,timerid;
	    if (telephone==='') {
	    	new UserPrompt('请输入手机号码');
	        return;
	    }
	    result=utils.checkTelephoneNumber(telephone);
	    if (!result.success) {
	    	new UserPrompt(result.msg);
	        return;
	    }
	    $.ajax({
	      type : "get",
	      dataType : "jsonp",
	      url : "http://223.202.119.13:8090/demo/getCode.do",
	      async : true,
	      data : {
	        'phone' : telephone,
	        'appid':paramJSON.app_key
	      },
	      success : function(data) {
	    	  if(data.result==='0'){
		          code.text((times = 60) + '秒');
		          code.attr('disabled', true);
		          timerid = setInterval(function() {
		            if (times == 1) {
		              clearInterval(timerid);
		              code.text('发送验证码');
		              code.attr('disabled', false);
		            }else
		              code.text(--times + '秒');
		          }, 1000);
		          smscode.focus();
	    	  }else{
	    		  if(data&&data.discripe)
	 			     new UserPrompt(data.discripe);
	    	  }
	      },
	      error:function(data){
	    	  if(data&&data.discripe)
			     new UserPrompt(data.discripe);
	      }
	    });
	  });	  
});